﻿#include "data.hpp"
#include "data.hpp"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <queue>
#include <utility>
#include <string_view>
#include <sstream>

using namespace maze;

/**
 *
 * @param file_name = "map_inp.ini"
 * @return
 */
inline bool Map::inpFrmFile(std::string_view file_name)
{
	std::ifstream file(file_name.data());
	if (!file.is_open()) {
		std::cerr << "无法打开文件: " << file_name << "\n";
		return false;
	}

	std::string line;
	// 读取地图的宽度和高度
	if (!std::getline(file, line) || line.empty() || line[0] == '#') {
		std::cerr << "文件格式错误：未找到地图尺寸信息。\n";
		file.close();
		return false;
	}

	std::istringstream size_stream(line);
	size_t width, height;
	if (!(size_stream >> width >> height)) {
		std::cerr << "文件格式错误：无法解析地图尺寸。\n";
		file.close();
		return false;
	}

	// 重新初始化地图
	m_width = width;
	m_height = height;
	m_map.resize(m_height, std::vector<Obj>(m_width, Obj()));

	// 读取顶点信息
	while (std::getline(file, line)) {
		// 跳过空行和注释行
		if (line.empty() || line[0] == '#') {
			continue;
		}

		// 去掉行首的空白字符（如果有的话）
		size_t start = line.find_first_not_of(" \t");
		if (start == std::string::npos) {
			continue;  // 空行或全是空白字符的行
		}
		line = line.substr(start);

		std::istringstream iss(line);
		std::string tag;
		iss >> tag;
		if (tag != "[Vertex]") {
			continue;
		}

		Locate loc;
		std::string attr_str;
		if (!(iss >> loc.first >> loc.second >> attr_str)) {
			std::cerr << "文件格式错误：无法解析顶点信息。\n";
			continue;
		}

		Attr attr;
		std::int32_t val = 0; // 默认值设为0
		if (attr_str == "ROAD") {
			attr = Attr::ROAD;
			if (iss >> val) {
				// 如果有值，则读取
			}
		}
		else if (attr_str == "WALL") {
			attr = Attr::WALL;
		}
		else if (attr_str == "TRAP") {
			attr = Attr::TRAP;
			if (iss >> val) {
				// 如果有值，则读取
			}
		}
		else if (attr_str == "TREASURE") {
			attr = Attr::TREASURE;
			if (iss >> val) {
				// 如果有值，则读取
			}
		}
		else {
			std::cerr << "未知属性: " << attr_str << "\n";
			continue;
		}

		// 检查坐标是否在地图范围内
		if (loc.first >= m_height || loc.second >= m_width) {
			std::cerr << "顶点坐标超出地图范围: " << loc.first << ", " << loc.second << "\n";
			continue;
		}

		// 将对象放置到地图中
		m_map[loc.first][loc.second] = Obj{ attr, val };
	}

	file.close();
	return true;
}


/**
 *
 * @param file_name = "map_out.ini"
 * @return
 */
inline bool Map::out2File(std::string_view file_name)
{
	std::ofstream file(file_name.data());
	if (!file.is_open()) {
		std::cerr << "无法打开文件: " << file_name << "\n";
		return false;
	}

	// 写入地图的宽度和高度
	file << m_width << " " << m_height << "\n";

	// 遍历地图，写入每个单元格的信息
	for (std::size_t i = 0; i < m_height; ++i) {
		for (std::size_t j = 0; j < m_width; ++j) {
			const Obj& obj = m_map[i][j];
			file << "[Vertex] " << i << " " << j << " ";
			switch (obj.attr) {
			case Attr::ROAD:
				file << "ROAD ";
				break;
			case Attr::WALL:
				file << "WALL ";
				break;
			case Attr::TRAP:
				file << "TRAP ";
				break;
			case Attr::TREASURE:
				file << "TREASURE ";
				break;
			default:
				file << "UNKNOWN ";
				break;
			}
			// 如果属性有值，则写入值
			if (obj.attr != Attr::WALL) {
				file << obj.val;
			}
			file << "\n";
		}
	}

	file.close();
	return true;
}